package com.liuhong.shoping.controller;


import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liuhong.shoping.common.ResponseMessage;
import com.liuhong.shoping.dao.UserInfoMapper;
import com.liuhong.shoping.entity.UserInfo;
import com.liuhong.shoping.utils.ExcelUtils;
import com.liuhong.shoping.vo.view.UserInfoPageVO;

import lombok.extern.slf4j.Slf4j;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liuhong
 * @since 2021-01-11
 */
@Slf4j
@RestController
@RequestMapping("/userInfo")
public class UserInfoController {
	
	@Autowired
	private UserInfoMapper userInfoMapper;
	
	/**
	 * @Param{ Map }
	 * 查询用户信息
	 */
	@PostMapping("getUserList")
    public ResponseMessage getUserList(@RequestBody UserInfoPageVO user) 
	{
		try {
			QueryWrapper<UserInfo> queryWrapperUserInfo = new QueryWrapper<>();
			queryWrapperUserInfo.lambda()
				.eq(StringUtils.isNotBlank(user.getOrgId()),UserInfo::getOrgId, user.getOrgId())
				.orderByDesc(UserInfo::getCreatedTime);
	        IPage<UserInfo>  userIPage=  userInfoMapper.selectPage(
	        		new Page<>(user.getCurrentPage(),user.getPageSize()),queryWrapperUserInfo);
	        List<UserInfo> list = userIPage.getRecords();
	        Integer selectCount = userInfoMapper.selectCount(queryWrapperUserInfo);
			return ResponseMessage.success(list,selectCount);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseMessage.fail();
    }
	
	/**
	 * @Param{ UserInfo entity }
	 * 添加用户
	 */
	@PostMapping("addUserInfo")
    public ResponseMessage addUserInfo(@RequestBody UserInfo user) 
	{
		try {
			user.setId(UUID.randomUUID().toString());
			user.setCreatedTime(LocalDateTime.now());
			int insertRows = userInfoMapper.insert(user);
			return ResponseMessage.success(insertRows,"插入成功");
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseMessage.fail();
		}
    }
	
	/**
	 * @Param{ UserInfo entity }
	 * 修改用户
	 */
	@PostMapping("updateUserInfo")
    public ResponseMessage updateUserInfo(@RequestBody UserInfo user) 
	{
		try {
			int updateRows = userInfoMapper.updateById(user);
			return ResponseMessage.success(updateRows,"修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseMessage.fail();
		}
    }
	
	/**
	 * @Param{ Map }
	 * 批量删除用户
	 */
	@PostMapping("deleteUserInfoByIds")
    public ResponseMessage deleteUserInfoByIds(@RequestBody Map<String, Object> map ) 
	{
		try {
			String batchIds = (String)map.get("bathIds");
			String[] split = batchIds.split(",");
			int deleteRows = userInfoMapper.deleteBatchIds(Arrays.asList(split));
			return ResponseMessage.success(deleteRows,"删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseMessage.fail();
		}
    }
	
	/**
	 * 导出用户信息
	 * @param request
	 * @param response
	 * @param userName
	 * @param userSex
	 */
	@GetMapping("exportUserInfoExcel")
	public void exportUserInfoExcel(
			HttpServletRequest request, 
			HttpServletResponse response,
			@RequestParam("userName")String userName,
			@RequestParam("userSex")String userSex
			) {
		try {
			QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
			queryWrapper.lambda()
			.eq(StringUtils.isNotBlank(userName),UserInfo::getUserName, userName)
			.eq(StringUtils.isNotBlank(userSex),UserInfo::getUserSex, userSex);
			
			List<UserInfo> list = userInfoMapper.selectList(queryWrapper);
			String[] columnNames = {
						"用户姓名","密码","性别","年龄","专业","学号/工号","工作单位","工作岗位",
						"行业","证件类型","证件号","用户中心权限","身份","创建时间","创建人"
					};
			String[] columns = {
						"userName","passWord","userSex","userAge","proFess","workNum",
						"workUnit","workPost","workIndustry","documentType","documentNum",
						"sysPermission","userIdentity","createdTime","creator"
					};
			String filename =  "用户信息"
				+ LocalDate.now()
				+ String.format("%02d",new Random().nextInt(999999));
			ExcelUtils.exportExcel(response, list, columnNames, columns, "用户信息", filename);
			log.debug("导出用户信息 [成功] [exportUserInfoExcel]");
		} catch (Exception e) {
			log.debug("导出用户信息 [失败] [exportUserInfoExcel]");
			e.printStackTrace();
		}
	}
}

